var STXMLParser = {};
STXMLParser.parse = function( responseText )
{
	// 텍스트 데이터를 XMLDOM 으로..
	if (window.DOMParser)
	{
		parser=new DOMParser();
		xmlDom=parser.parseFromString( responseText,"text/xml");
	}
	else // Internet Explorer
	{
		xmlDom=new ActiveXObject("Microsoft.XMLDOM");
		xmlDom.async=false;
		xmlDom.loadXML(responseText);
	}
	
	if( xmlDom.firstChild.nodeType != STXML.PROCESSING_INSTRUCTION_NODE )
	{
		return new STXML( xmlDom.firstChild );
	}
	return new STXML( xmlDom.childNodes[1] );
}



var STXML = function( dom )
{
	this._parse_.call( this, dom);
	var _name = dom.nodeName;
	this.name = function()
	{
		return _name;
	}
}

STXML.ELEMENT_NODE = 1;
STXML.ATTRIBUTE_NODE = 2;
STXML.TEXT_NODE = 3;
STXML.CDATA_SECTION_NODE = 4;
STXML.ENTITY_REFERENCE_NODE = 5;
STXML.ENTITY_NODE = 6;
STXML.PROCESSING_INSTRUCTION_NODE = 7;
STXML.COMMENT_NODE = 8;
STXML.DOCUMENT_NODE = 9;
STXML.DOCUMENT_TYPE_NODE = 10;
STXML.DOCUMENT_FRAGMENT_NODE = 11;
STXML.NOTATION_NODE = 12;

STXML.prototype = {
	_parse_ : function( dom )
	{
		
		// att
		if( dom.attributes != null && dom.attributes.length > 0 )
		{
			for( var j=0; j<dom.attributes.length; j++ )
			{
				this[ "@"+dom.attributes[j].nodeName ] = dom.attributes[j].nodeValue;
			}
		}
		
		// children
		var _children = dom.childNodes;
		var _len = _children.length;
		for( var i=0; i<_len ; i++ )
		{
			var _child = _children[i];
			
			var _list = this[ _child.nodeName ] = this[ _child.nodeName ]||[];
				
			if( _child.nodeType == STXML.TEXT_NODE && String( _child.data ).trim() == "" )
			{
				continue;
			}else if( _child.nodeType == STXML.TEXT_NODE || _child.nodeType == STXML.CDATA_SECTION_NODE )
			{
				_list.push( _child.data+"" );
			}else if( _child.nodeType == STXML.ELEMENT_NODE )
			{
				_list.push( new STXML( _child ) );
			}
		}
		
		// filtering
		for( var key in this )
		{
			if( this[key][ "length" ] == undefined ) continue;
			if( this[key].length == 0 ) delete this[ key ];
			else if( this[key].length == 1 )
			{
				// 1개짜리 리스트
				if( this[key][0] instanceof STXML == false )
				{
					if( key == "#text" || key == "#cdata-section" )
					{
						this.toString = function( t )
						{
							return ( function(){ return t; } );
						}( this[key][0] );
						delete this[key];
					}else{
						this[key] = this[key][0];
					}
				}
			}
		}
		
		
	}
}